import 'package:flutter/material.dart';
import 'package:hugeicons/hugeicons.dart';
import 'package:provider/provider.dart';
import 'package:tool/page/file/m/file_item_model.dart';
import '../../base/base_state.dart';
import '../../resources.dart';

import 'vm/file_manager_model.dart';

/// created by hcl
/// on 2024/5/9
/// FileManagerPage
/// desc
class FileManagerPage extends StatefulWidget {
  const FileManagerPage({super.key});

  @override
  State<StatefulWidget> createState() => _FileManagerState();
}

class _FileManagerState extends BaseState<FileManagerModel, FileManagerPage> {
  @override
  String get title => '文件管理';

  @override
  VoidCallback get baseBack => () {
        if ((model.isOuter ? realGenDir : model.genDir) ==
            model.dir?.path) {
          Nav.pop(result: null);
        } else {
          model.goParent();
        }
      };

  @override
  FileManagerModel initModel() {
    return FileManagerModel();
  }

  @override
  Widget bodyWidget(BuildContext context) {
    return Consumer<FileManagerModel>(
        builder: (BuildContext context, model, Widget? child) {
      return Padding(
        padding: EdgeInsets.all(8.ss),
        child: Column(
          children: [
            _buildTop(),
            Expanded(
              child: _buildList(),
            ),
          ],
        ),
      );
    });
  }

  Widget _buildTop() {
    String tempDir = '';
    if (model.dir != null && model.dir!.path.contains(model.genDir)) {
      tempDir = model.dir!.path.replaceAll(model.genDir, word.app_name);
    } else {
      tempDir = model.dir?.path ?? '';
    }
    return InkWell(
      onTap: () {
        model.goParent();
      },
      onLongPress: () {
        model.changeInnerOuter();
      },
      borderRadius: BorderRadius.circular(8.ss),
      child: Padding(
        padding: EdgeInsets.all(8.ss),
        child: Row(
          children: [
            HugeIcon(
                icon: HugeIcons.strokeRoundedFolderImport,
                color: ColorUtils.color_333333,
                size: 16.ss),
            SizedBox(width: 5.ss),
            Expanded(
                child: SingleChildScrollView(
                    scrollDirection: Axis.horizontal,
                    child: Text(tempDir, style: normal312))),
          ],
        ),
      ),
    );
  }

  Widget _buildList() {
    return ListView.separated(
        padding: EdgeInsets.zero,
        shrinkWrap: true,
        itemCount: model.list.length,
        separatorBuilder: (context, index) {
          return Padding(
            padding: EdgeInsets.symmetric(horizontal: 8.ss),
            child: Divider(
                color: ColorUtils.color_999999.withOpacity(0.3), height: 1),
          );
        },
        itemBuilder: (context, index) {
          FileItemModel itemModel = model.list[index];
          return InkWell(
            onTap: () {
              model.goNext(itemModel);
            },
            borderRadius: BorderRadius.circular(8.ss),
            child: Padding(
              padding: EdgeInsets.all(8.ss),
              child: Row(
                children: [
                  HugeIcon(
                      icon: itemModel.isDir
                          ? HugeIcons.strokeRoundedFolderExport
                          : HugeIcons.strokeRoundedFile02,
                      color: ColorUtils.color_333333,
                      size: 16.ss),
                  SizedBox(width: 5.ss),
                  Expanded(
                    child: Text(
                      itemModel.name,
                      style: normal616,
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                    ),
                  ),
                ],
              ),
            ),
          );
        });
  }
}
